binary search implementation sortings strings *1800

Please click on ads to support us..

Python Code:

import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

t = int(input())
for tidx in range(t):
    s = input().decode().strip()
    l = len(s)
    l1 = [0]*26
    l2 = [-1]*26
    u = 0
    ans2 = ""
    for i in range(l-1, -1, -1):
        j = s[i]
        q = ord(j)-ord('a')
        if l1[q] == 0:
            u += 1
            l2[q] = len(ans2)
            ans2 = j+ans2
        l1[q] += 1
    ls = sum([l1[q]//(u-l2[q]) for q in range(26) if l2[q]>=0])
    ans1 = s[:ls]
    check = ans1
    checksum = ans1
    for j in ans2:
        check = "".join([x for x in check if x != j])
        checksum += check
    if checksum != s:
        print(-1)
    else: print(ans1, ans2)
        

C++ Code:

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "./debug.h"
#else
#define debug(...)
#endif
#define int long long

using namespace std;

const int inf = 1e18;
const int N = 3e5 + 5;
const int mod = 998244353;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    map<char, int> m;
    map<char, int> cnt;
    for (int i = 0; i < n; i++) {
        m[s[i]] = i;
        cnt[s[i]]++;
    }
    string t = s;
    sort(t.begin(), t.end(), [&](auto x, auto y) {
        return m[x] < m[y];
    });
    vector<char> order;
    for (char c : t) {
        if (order.empty() || order.back() != c) {
            order.push_back(c);
        }
    }
    auto get = [&](char c) {
        for (int i = 0; i < order.size(); i++) {
            if (order[i] == c)
                return i + 1;
        }
        return 0ll;
    };
    map<char, int> temp = cnt;
    for (auto& x : temp) {
        int id = get(x.first);
        x.second /= id;
    }
    string res;
    for (char c : s) {
        if (--temp[c] >= 0)
            res.push_back(c);
    }

    auto encrypt = [&]() {
        string cur, here = res;
        for (auto p : order) {
            cur += here;
            string nxt;
            for (char c : here) {
                if (c != p) {
                    nxt.push_back(c);
                }
            }
            here = nxt;
        }
        return cur;
    };
    string encrypted = encrypt();
    if (encrypted != s) {
        cout << "-1\n";
        return;
    }
    cout << res << " ";
    for (char c : order) {
        cout << c;
    }
    cout << "\n";
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.out", "r", stdin);
    freopen("output.out", "w", stdout);
    freopen("error.out", "w", stderr);
#endif
    int T = 1;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        // cout << "Case #" << t << ": ";
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes